package top.api.service.impl;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import top.api.common.ResUlt;
import top.api.domain.dto.ClassAddDto;
import top.api.domain.dto.ClassEditDto;
import top.api.domain.dto.PageDto;
import top.api.domain.entity.Class;
import top.api.domain.entity.Student;
import top.api.domain.entity.User;
import top.api.domain.vo.PageVo;
import top.api.enums.CommonStatusEnum;
import top.api.mapper.StudentMapper;
import top.api.service.ClassService;
import top.api.mapper.ClassMapper;
import org.springframework.stereotype.Service;
import top.api.utils.AssertUtil;
import top.api.utils.BeanCopyUtils;

import java.util.List;

/**
* @author 27465
* @description 针对表【class】的数据库操作Service实现
* @createDate 2023-12-16 23:06:21
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class>
    implements ClassService{
    private final StudentMapper studentMapper;

    @Override
    public ResUlt<?> pageList(PageDto pageDto, Integer id, String classname) {
        Page<Class> pageInfo = new Page<>(pageDto.getPageNum(),pageDto.getPageSize());

        LambdaQueryWrapper<Class> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotNull(id),Class::getId,id);
        queryWrapper.like(StringUtils.isNotBlank(classname),Class::getClassname,classname);

        baseMapper.selectPage(pageInfo,queryWrapper);

        PageVo<Class> pageVo = new PageVo<>(pageInfo.getRecords(),pageInfo.getTotal());

        return ResUlt.success(pageVo);
    }

    @Override
    public ResUlt<?> getClassInfo(Integer id) {
        return ResUlt.success(baseMapper.selectById(id));
    }

    @Override
    public ResUlt<?> addClass(ClassAddDto classAddDto) {
        Class aClass = BeanCopyUtils.copyBean(classAddDto, Class.class);

        // 添加no
        aClass.setCno(IdWorker.getIdStr());

        // 判断班级名是否存在
        Long count = baseMapper.selectCount(Wrappers.<Class>lambdaQuery()
                .eq(Class::getClassname, classAddDto.getClassname()));
        AssertUtil.isTrue(count > 0,"班级名已存在");

        int flag = baseMapper.insert(aClass);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> editClass(ClassEditDto classEditDto) {
        Class aClass = BeanCopyUtils.copyBean(classEditDto, Class.class);

        // 判断用户名是否存在 不包括自己
        Long count = baseMapper.selectCount(Wrappers.<Class>lambdaQuery()
                .eq(Class::getClassname, classEditDto.getClassname())
                .ne(Class::getId,classEditDto.getId()));
        AssertUtil.isTrue(count > 0,"班级名已存在");

        int flag = baseMapper.updateById(aClass);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> delClass(List<Integer> ids) {
        // 不允许删除已被学生表绑定的班级
        Long count = studentMapper.selectCount(Wrappers.<Student>lambdaQuery()
                .in(Student::getClassno, ids));

        AssertUtil.isTrue(count > 0,"已有学生绑定班级");

        int flag = baseMapper.deleteBatchIds(ids);

        return ResUlt.success("删除成功: ["+count+"]");
    }


}




